package indi.zhifa.study2025.test.rabbitmq.dm.producer;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import indi.zhifa.engine.core.common.utils.TimeUtil;
import indi.zhifa.study2025.test.rabbitmq.common.Constant;
import indi.zhifa.study2025.test.rabbitmq.common.entity.OrderMsg;
import indi.zhifa.study2025.test.rabbitmq.common.utils.NWRabbitUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

@Slf4j
public class DMProcedureApplication {

    public static void init(Channel pChannel) throws IOException {
        // 声明一个 direct 类型的死信交换机
        pChannel.exchangeDeclare(Constant.DM_EXCHANGE_NAME, BuiltinExchangeType.DIRECT, true);
        // 定义和死信队列相关的参数
        Map<String,Object> params = new HashMap<>();
        // 消息5分钟过期
        params.put("x-message-ttl",5*60*1000);
        // 死信交换机名字
        params.put("x-dead-letter-exchange", Constant.DM_EXCHANGE_NAME);
        // 死信交换机的键
        params.put("x-dead-letter-routing-key", Constant.ROUTE_QUE_DM_ORDER_EXPIRE);

        // 声明 死信队列
        pChannel.queueDeclare(Constant.DM_QUE_NAME, true, false, false, null);

        // 声明一个普通队列，接入死信交换机
        pChannel.queueDeclare(Constant.NW_QUE_DM_NAME, true, false, false, params);

        // 将 DLQ 绑定到之前声明的 DLX
        pChannel.queueBind(Constant.DM_QUE_NAME, Constant.DM_EXCHANGE_NAME, Constant.ROUTE_QUE_DM_ORDER_EXPIRE);
    }

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = null;
        Connection connection = null;
        final String queName;
        queName = Constant.NW_QUE_DM_NAME;
        //queName = Constant.NW_QUE_NAME;
        try{
            connection = NWRabbitUtils.getConnection();
            channel = connection.createChannel();
            init(channel);
            for (int i = 0; i < 22; i++) {
                String code = RandomUtil.randomString(8);
                OrderMsg orderMsg = new OrderMsg();
                orderMsg.setIdx(i+1);
                orderMsg.setCode(code);
                orderMsg.setBillTime(LocalDateTime.now());
                orderMsg.setBillTimeSecond(TimeUtil.nowSecond());
                orderMsg.setAmount(RandomUtil.randomBigDecimal(BigDecimal.valueOf(10), BigDecimal.valueOf(1000)));
                String jsonMsg = JSON.toJSONString(orderMsg);

                AMQP.BasicProperties abp = new AMQP.BasicProperties.Builder()
                        .priority(2)
                        .build();

                channel.basicPublish("", queName, null, jsonMsg.getBytes());
                log.info("push msg "+orderMsg);
                Thread.sleep(200);
            }
        }catch (Exception e){
            System.out.println(e);
        }finally {
            channel.close();
            connection.close();
        }
    }
}
